<script lang="ts" setup>
import {reactive, ref, watch} from 'vue'
import {Fold, Open, TurnOff, Close} from "@element-plus/icons-vue";
import {ConnPhone, hideWindow, InstallApk, Msg, UnInstallApk} from "./go";

const LocalStorageIsTop = "LocalStorageIsTop"
const LocalStorageIsWalk = "LocalStorageIsWalk"
const alwaysTop = ref(localStorage.getItem(LocalStorageIsTop) == "true")
const alwaysWalk = ref(localStorage.getItem(LocalStorageIsWalk) == "true")
let canEdit = ref(true)
let isDoing = ref(false)

const connPhone = async () => {
  try {
    canEdit.value = false
    await ConnPhone(alwaysTop.value, alwaysWalk.value)
  } catch (e) {
    await Msg("操作失败", `${e}`)
  } finally {
    canEdit.value = true
  }
}

watch(alwaysTop, (c) => {
  localStorage.setItem(LocalStorageIsTop, "" + c)
})
watch(alwaysWalk, (c) => {
  localStorage.setItem(LocalStorageIsWalk, "" + c)
})

const closeWin = async () => {
  hideWindow()
}

const installApk = async () => {
  try {
    isDoing.value = true
    await InstallApk()
  } catch (e) {
    await Msg("操作失败", `${e}`)
  } finally {
    isDoing.value = false
  }
}

const unInstallApk = async () => {
  try {
    isDoing.value = true
    await UnInstallApk()
  } catch (e) {
    await Msg("操作失败", `${e}`)
  } finally {
    isDoing.value = false
  }
}

</script>
<template>
  <el-container style="--webkit-app-region: no-drag;" class="detailContainer" direction="vertical">
    <el-button :icon="Close" style="position: absolute;top: 20px;right: 30px;padding: 10px;" type="danger" size="small"
               @click="closeWin()" round></el-button>
    <el-main>
      <el-row>
        <el-switch
            v-model="alwaysTop"
            :disabled="!canEdit"
            active-text="总是置顶"
        />
        <span style="width: 32px"/>
        <el-switch
            :disabled="!canEdit"
            v-model="alwaysWalk"
            active-text="总是唤醒"
        />
      </el-row>
    </el-main>
    <el-footer style="display: flex;">
      <el-space style="flex: 1" alignment="start">
        <el-button :disabled="isDoing" size="small" @click="installApk()" round>安装apk</el-button>
        <el-button :disabled="isDoing" size="small" @click="unInstallApk()" round>卸载apk</el-button>
      </el-space>
      <el-button type="primary" size="small" :disabled="!canEdit" @click="connPhone()" round>连接手机</el-button>
<!--      <el-button data-wml-window="Close" type="danger" size="small"  round>关闭</el-button>-->
    </el-footer>
  </el-container>
</template>

<style>
.detailContainer {
  overflow: hidden;
  padding: 10px;
}

.el-container {
  --webkit-app-region: drag !important;
}

.el-button, .el-switch {
  --webkit-app-region: no-drag !important;
}
</style>
